package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.behavior.mapper.BehaviorEntryMapper;
import com.usian.behavior.mapper.BehaviorFollowMapper;
import com.usian.behavior.service.BehaviorFollowService;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.behavior.dots.FollowBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BehaviorFollowServiceImpl extends BaseServiceImpl<BehaviorFollowMapper, ApFollowBehavior> implements BehaviorFollowService {
    @Autowired
    private BehaviorEntryMapper entryMapper;
    @Override
    public ResponseResult loginBehavior(Integer userId) {
        if (userId==null)return errorParam;
        ApBehaviorEntry behaviorEntry = new ApBehaviorEntry();
        behaviorEntry.setEntryId(userId);
        behaviorEntry.setCreatedTime(new Date());
        behaviorEntry.setType((short)1);
        entryMapper.insert(behaviorEntry);
        return successParam;
    }
    @Override
    public ResponseResult followBehavior(FollowBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getFollowId()==null||dto.getUserId()==null)return errorParam;
        //查询行为实体
        ApBehaviorEntry behaviorEntry = entryMapper.selectOne(Wrappers.<ApBehaviorEntry>lambdaQuery().eq(ApBehaviorEntry::getEntryId, dto.getUserId()));
        ApFollowBehavior followBehavior = new ApFollowBehavior();
        followBehavior.setArticleId(dto.getArticleId());
        followBehavior.setFollowId(dto.getFollowId());
        followBehavior.setEntryId(behaviorEntry.getId());
        followBehavior.setCreatedTime(new Date());
        save(followBehavior);
        return successParam;
    }


}
